// import {getCacheSync} from './cache.js'
//登录判断，没有第一个页面跳转登录页
// 拦截tabbar页面跳转
// uni.addInterceptor('switchTab', {
// 	invoke (e) {
// 		if (!getCacheSync('userInfo')) {
// 			uni.redirectTo({
// 				url: '/pages/login/index/index'
// 			});
// 			return false
// 		}else{
// 			return true
// 		}
// 	},
// 	success (e) {
// 		// console.log(e)
// 	}
// })

import permision from "@/common/js_sdk/permission.js"
// console.log(permision);

/**
 * 获取手机摄像头和图册权限
 * */
const cameraAuthority = () =>{
	return new Promise(async (rel,rej)=>{
		var platform = getApp().globalData.mySystemInfo.osName || 'android';
		// console.log(platform)
		if(platform == 'ios'){
			let camera = permision.judgeIosPermission("camera");//判断ios是否给予摄像头权限
			//ios相册没权限，系统会自动弹出授权框
			//let photoLibrary = permision.judgeIosPermission("photoLibrary");//判断ios是否给予相册权限
			if(camera){
				rel();
			}else{
				rej('需要开启相机使用权限');
			}
		}else{
			let camera = await permision.requestAndroidPermission("android.permission.CAMERA");//判断安卓是否给予摄像头权限
			let photoLibrary = await permision.requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE");//判断安卓是否给予相册权限
			if(camera == 0 || photoLibrary == 0){
				rej('请同时开启相机和相册的使用权限');
			}else if(camera == -1 || photoLibrary == -1){
				//永久拒绝
				rej(false)
			}else{
				rel();
			}
		}
	})
}

/**
 * 限安卓
 * 是否打开了手机定位
 * */
const adressOpen = () =>{
	return permision.checkSystemEnableLocation();
}

/**
 * 限安卓
 * 获取位置信息权限
 * */
const adressAuthority = () =>{
	return new Promise(async (rel,rej)=>{
		let adress = await permision.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION");//判断安卓是否有获取位置信息权限
		if(adress == 0){
			rej('请开启定位权限');
		}else if(adress == -1){
			//永久拒绝
			rej(false)
		}else{
			rel();
		}
	})
}

/**
 * 限安卓
 * 获取拨打电话权限
 * */
const cellPhoneAuthority = () =>{
	return new Promise(async (rel,rej)=>{
		let adress = await permision.requestAndroidPermission("android.permission.CALL_PHONE");//拨打电话权限
		if(adress == 0){
			rej('请开启拨打电话权限');
		}else if(adress == -1){
			//永久拒绝
			rej(false)
		}else{
			rel();
		}
	})
}

/**
 * 跳转到手机开启权限的界面
 * */
const permissionSetting = () =>{
	permision.gotoAppPermissionSetting();
}

// module.exports = {
// 	cameraAuthority,
// 	permissionSetting,
// 	adressAuthority,
// 	adressOpen,
// 	cellPhoneAuthority
// }
export{
	cameraAuthority,
	permissionSetting,
	adressAuthority,
	adressOpen,
	cellPhoneAuthority
}
